package com.william.jdbcplus.core.jdbc.handle.impl;

import com.william.jdbcplus.core.jdbc.handle.RsHandler;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class MapHandler implements RsHandler<Map<String, Object>> {
    private static MapHandler instance = new MapHandler();

    private MapHandler() {
    }

    public static MapHandler getInstance() {
        return instance;
    }

    @Override
    public Map<String, Object> handle(ResultSet rs) throws SQLException {
        Map<String, Object> map = new HashMap<>();
        ResultSetMetaData md = rs.getMetaData();
        int columnCount = md.getColumnCount();
        while (rs.next()) {
            for (int i = 1; i <= columnCount; i++) {
                map.put(md.getColumnName(i), rs.getObject(i));
            }
        }
        return map;
    }
}
